﻿using System;
using com.yoozoo.gta.Gameplay.RTS;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Yoozoo.Gameplay.RTS.Proto;
using Yoozoo.UI.YGUI;

namespace Yoozoo.Gameplay.RTS
{
    public class MarchHudLod0 : MonoBehaviour
    {


        public long Tid = 0L;
        /// <summary>
        /// 玩家名
        /// </summary>
        public TMP_Text PlayerNameText;

        public Transform PlayerNameBg;

        public TMP_Text RenownNameText;
        public Transform RenownNameBg;
        
        public YImage AllianceBg;
        public YImage AllianceFrame;
        public YImage AllianceIcon;
        
        /// <summary>
        /// 玩家称号
        /// </summary>
        public YImage PlayerTitle;
        private bool PlayerTitleEnable  = false;
        
        public GpuPlayerNameEntity GpuPlayerName;
        public GpuPlayerNameEntity GpuRenownName;
        public HudBase hudRef;

        /// <summary>
        /// 采集的图标
        /// </summary>
        public YImage StateIcon;

//        public Transform StateSleepTran;

        public YImage StateIconBg;

        /// <summary>
        /// 英雄信息
        /// </summary>
        public CommanderHead CommanderHead;

        public Collider colldier;

        private int[] layerDiff;
        private SpriteRenderer[] spriteRenderers;
        private string marchName = "";
        private string renownName = "";


        public GameObject killTipRoot; // 击杀提示根节点
        public TMP_Text txtKillTip; // 击杀提示文本
        public Animation killTipAnim; // 击杀提示动画组件
        private bool isKillTipShow = false;// 击杀提示是否显示 用于回池的时候重置动画

        /// <summary>
        /// 其他人的战斗信息
        /// </summary>
        private bool ColldierEnable = true;
        private bool AllianceBgEnable  = false;
        public void Dispose()
        {
            marchName = "";
            renownName = "";
            hudRef = null;
            GpuHudFacade.GpuFacade.RecoverPlayerNameEntity(GpuPlayerName);
            GpuHudFacade.GpuFacade.RecoverPlayerNameEntity(GpuRenownName);
            GpuPlayerName = null;
            GpuRenownName = null;
            PlayerNameText.text = string.Empty;
            RenownNameText.text = string.Empty;
            SetDefaultTitleActive();
            StopKillTipAnim();
        }
        public void SetCollectTeamIcon(int team,long tid)
        {
            if (team == 0)
            {
                ArmyManager.Instance.ResourceManager.SetParkIcon("icon_do_6",StateIcon);
            }
            else if (team == 1)
            {
                ArmyManager.Instance.ResourceManager.SetParkIcon("icon_do_6_blue", StateIcon);
            }
            else
            {
                ArmyManager.Instance.ResourceManager.SetParkIcon("icon_do_6_red", StateIcon);
            }

            Tid = tid;
        }

      
        public void SetColliderEnable(bool enable)
        {
            if (ColldierEnable != enable)
            {
                colldier.enabled = enable;
                ColldierEnable = enable;
            }
        }
        
        
        public void SetSoldierTypeIconVisible(bool visible)
        {
            //if (visible)
            //{
            //    playerNameLayout.padding.left = -25;
            //}
            //else
            //{
            //    playerNameLayout.padding.left = 10;
            //}
            //soldierTypeIcon.gameObject.SetActive(visible);
        }

        public void SetNickName(string nickName)
        {
            if (string.IsNullOrEmpty(nickName)) return;
            if (GpuPlayerName != null && marchName != nickName)
            {
                //会触发更新么
                marchName = nickName;
                GpuPlayerName.SetName(nickName,new PlayerNameAlterContext()
                {
                    refreshColor = true,
                    color = PlayerNameText.color
                },true);
            }
            else
            {
                marchName = nickName;
                ShowNickName();
            }
        }

        private void SetAllianceIconPosition()
        {
            var position = AllianceBg.rectTransform.localPosition;
            var width = PlayerNameText.GetPreferredValues().x;
            AllianceBg.rectTransform.localPosition = new Vector3(- width/2 - 10,position.y,position.z);
        }
        
        private void SetPlayerTitleIconPosition()
        {
            PlayerTitle.rectTransform.position = PlayerNameText.transform.position;
            var position = PlayerTitle.rectTransform.localPosition;
            var width = PlayerNameText.GetPreferredValues().x;
            PlayerTitle.rectTransform.localPosition = new Vector3(- width/2 - 25,position.y,position.z);
        }
        //只设置，不显示
        public void SetNickNameContent(string nickName)
        {
            marchName = nickName;
        }

        public void SetRenownNameContent(string name)
        {
            renownName = name;
        }
        
        public void RemoveNickName()
        {
            if (GpuPlayerName != null)
            {
                GpuPlayerName?.SetVisible(false);
            }
        }
        /// <summary>
        /// 暂时使用PlayerNameText里的color
        /// </summary>
        public void ShowNickName()
        {
            if (GpuPlayerName == null && !string.IsNullOrEmpty(marchName))
            {
                int id = 1;
                if (hudRef?.Team == (int)MarchTeam.Self)
                {
                    id = 2;
                }
                GpuPlayerName = GpuHudEntityHelper.ShowPlayerNameHud(id, PlayerNameText.transform.position, PlayerNameText.color,transform.localScale.x*170, marchName);
                GpuPlayerName.SetPosition(PlayerNameText.transform.position,true);
            }
            else
            {
                GpuPlayerName?.SetVisible(true);
                GpuPlayerName?.SetPosition(PlayerNameText.transform.position,true);
                GpuPlayerName?.SetColor(PlayerNameText.color);
                GpuPlayerName?.SetScale(transform.localScale.x*170);
            }
            
            if (AllianceBgEnable)
            {
                SetAllianceIconPosition();
            }
            if (PlayerTitleEnable)
            {
                SetPlayerTitleIconPosition();
            }
        }

        //这部分可以搞到PlayerNameEntity中去
        public void SetNickNamePosition(bool force = false)
        {
            GpuPlayerName?.SetPosition(PlayerNameText.transform.position,force);
            if (PlayerTitleEnable)
            {
                SetPlayerTitleIconPosition();
            }
        }
        public void SetNickNameColor(Color color)
        {
            GpuPlayerName?.SetColor(color);
        }
        public void SetNickNameScale(float scale)
        {
            GpuPlayerName?.SetScale(scale*170);
            GpuPlayerName?.SetPosition(PlayerNameText.transform.position);
            if (PlayerTitleEnable)
            {
                SetPlayerTitleIconPosition();
            }
        }
        /// <summary>
        /// 设置名字可见性
        /// </summary>
        /// <param name="visable"></param>
        public void SetNickNameVisiable(bool visable)
        {
            if (visable)
            {
                ShowNickName();
            }
            else
            {
                RemoveNickName();
            }
        }

        public void SetRenownNameVisiable(bool visable)
        {
            if (visable)
            {
                ShowRenownName();
            }
            else
            {
                RemoveRenownName();
            }
        }
        
        public void RemoveRenownName()
        {
            if (GpuRenownName != null)
            {
                GpuRenownName?.SetVisible(false);
            }
        }
        
        public void ShowRenownName()
        {
            if (GpuRenownName == null && !string.IsNullOrEmpty(renownName))
            {
                int id = 1;
                if (hudRef?.Team == (int)MarchTeam.Self)
                {
                    id = 2;
                }
                GpuRenownName = GpuHudEntityHelper.ShowPlayerNameHud(id, RenownNameText.transform.position, RenownNameText.color,transform.localScale.x*170, renownName);
                GpuRenownName.SetPosition(RenownNameText.transform.position,true);
            }
            else
            {
                GpuRenownName?.SetVisible(true);
                GpuRenownName?.SetPosition(RenownNameText.transform.position,true);
                GpuRenownName?.SetColor(RenownNameText.color);
                GpuRenownName?.SetScale(transform.localScale.x*170);
            }
        }
        
        public void SetRenownNamePosition(bool force = false)
        {
            GpuRenownName?.SetPosition(RenownNameText.transform.position,force);
        }
        public void SetRenownNameColor(Color color)
        {
            GpuRenownName?.SetColor(color);
        }
        public void SetRenownNameScale(float scale)
        {
            GpuRenownName?.SetScale(scale*170);
            GpuRenownName?.SetPosition(RenownNameText.transform.position);
        }

        public void SetAllianceIconVisible(bool visible)
        {
            if (AllianceBgEnable != visible)
            {
                if (AllianceBg != null)
                {
                    AllianceBg.gameObject.SetActive(visible);
                    AllianceBgEnable = visible;

                    if (visible)
                    {
                        SetAllianceIconPosition();
                    }
                }
            }
        }
        
        public void SetPlayerTitleIconVisible(bool visible)
        {
            if (PlayerTitleEnable != visible || PlayerTitle.gameObject.activeSelf != visible)
            {
                if (PlayerTitle != null)
                {
                    PlayerTitle.gameObject.SetActive(visible);
                    PlayerTitleEnable = visible;

                    if (visible)
                    {
                        SetPlayerTitleIconPosition();
                    }
                }
            }
        }

        public void SetLogo(RtsServerInfo serverInfo)
        {
            if (AllianceIcon)
            {
                ArmyManager.Instance.ResourceManager.SetAllianceIcon(serverInfo.Logo, AllianceIcon);
                ColorUtility.TryParseHtmlString(serverInfo.LogoColor,out Color color);
                AllianceIcon.color = color;
            }
            if (AllianceFrame)
            {
                ArmyManager.Instance.ResourceManager.SetAllianceIcon(serverInfo.LogoFrame, AllianceFrame);
            }
            if (AllianceBg)
            {
                ArmyManager.Instance.ResourceManager.SetAllianceIcon(serverInfo.LogoBg, AllianceBg);
                
                ColorUtility.TryParseHtmlString(serverInfo.LogoBgColor,out Color color);
                AllianceBg.color = color;
            }
        }

        public void SetPlayerTitle(RtsServerInfo serverInfo)
        {
            if (PlayerTitle)
            {
                ArmyManager.Instance.ResourceManager.SetAllianceIcon(serverInfo.roleTitle, PlayerTitle);
            }
        }

        public void SetDefaultTitleActive()
        {
            PlayerTitle.sprite = null;
            PlayerTitle.gameObject.SetActive(false);
        }

        public void SetKillTip(int killNum, string killTip)
        { 
            if (killTipRoot && txtKillTip)
            {
                isKillTipShow = killNum > 0; 
                txtKillTip.text = killTip;
                if (killTipAnim && isKillTipShow)
                {
                    killTipAnim.Stop();
                    killTipAnim.Play("ani_march_troopKill_into"); 
                }
                else
                {
                    StopKillTipAnim();
                }
            }
        }

        public void StopKillTipAnim()
        {
            if (isKillTipShow)
            {
                isKillTipShow = false;
                killTipAnim.Stop();
                killTipRoot.transform.GetChild(0).gameObject.SetActive(false); // 简单的重置一下节点
            }
        }
    }
}
